pdf <- function(x) {exp(-x)}
curve(pdf, from = 0, to=100, n=1000)
pdf <- function(x) {exp(-x)}
curve(pdf, from = 0, to=10, n=1000)
pdf <- function(x) {exp(-x)}
curve(pdf, from = 0, to=0.01, n=1000)
pdf <- function(x) {exp(-x)}
curve(pdf, from = 0, to=10, n=1000)
pdf <- function(x) {exp(-x)}
curve(pdf, from = 0, to=6, n=1000)
cdf <- function(x) {-exp(-x)}
curve(cdf, from = 0, to=6, n=1000)
cdf <- function(x) {-exp(-x) + 1}
curve(cdf, from = 0, to=6, n=1000)
pdf <- function(x) {exp(-x)}
curve(pdf, from = 0, to=6, n=1000)
pdf2 <- function(x) {(2/3)x^(-1/3)}  # for 0 <= x <= 1
curve(pdf1, from = 0, to=1, n=1000)
pdf2 <- function(x) {(2/3)x^(-1/3)}  # for 0 <= x <= 1
curve(pdf2, from = 0, to=1, n=1000)
pdf2 <- function(x) {(2/3)*(x^(-1/3))}  # for 0 <= x <= 1
curve(pdf2, from = 0, to=1, n=1000)
cdf2 <- function(x) {x^(2/3)}
curve(cdf2, from = 0, to=1, n=1000)
pdf2 <- function(x) {(2/3)*(x^(-1/3))}  # for 0 <= x <= 1
curve(pdf2, from = 0, to=1, n=1000)
cdf2 <- function(x) {x^(2/3)}
curve(cdf2, from = 0, to=1, n=1000)
pi
npdf <-function(x, mu, sigma) {
((2*pi*sigma^2)^(1/2))exp(-((x-mu)^2)/(2*sigma^2))
}
npdf <-function(x, mu, sigma) {
((2*3.14159*sigma^2)^(1/2))exp(-((x-mu)^2)/(2*sigma^2))
}
npdf <-function(x, mu, sigma) {
((2*pi*sigma^2)^(1/2))*exp(-((x-mu)^2)/(2*sigma^2))
}
npdf <-function(x) {
((2*pi*sigma^2)^(1/2))*exp(-((x-mu)^2)/(2*sigma^2))
}
mu<-0
sigma<-1
curve(npdf, from = -3, to=3, n=1000)
dnorm(0)
x<-(seq(-3,3,length.out=1000))
y<-(seq(0,1,length.out=1000))
plot(x,y, type = "n", main=paste("Prior and Posterior Distributions"), xlab=("θ"), ylab=("density"))
lines((seq(-3,3,length.out=1000)), dnorm(seq(-3,3,length.out=1000)), type="l", lwd=3, col="steelblue")
plot(x,y, type = "n", main=paste("Prior and Posterior Distributions"), xlab=("θ"), ylab=("density"))
lines((seq(-3,3,length.out=1000)), dnorm(seq(-3,3,length.out=1000)), type="2", lwd=1, col="steelblue")
plot(x,y, type = "n", main=paste("Prior and Posterior Distributions"), xlab=("θ"), ylab=("density"))
lines((seq(-3,3,length.out=1000)), dnorm(seq(-3,3,length.out=1000)), type="l", lwd=1, lty=1, col="steelblue")
lines((seq(-3,3,length.out=1000)), dnorm(seq(-3,3,length.out=1000), mean=1, sd=0.3), type="l", lwd=1, lty=2, col="maroon")
# plot normal curves on one figure
x<-(seq(-3,3,length.out=1000))
y<-(seq(0,1.4,length.out=1000))
plot(x,y, type = "n", main=paste("Prior and Posterior Distributions"), xlab=("θ"), ylab=("density"))
lines((seq(-3,3,length.out=1000)), dnorm(seq(-3,3,length.out=1000)), type="l", lwd=1, lty=1, col="steelblue")
lines((seq(-3,3,length.out=1000)), dnorm(seq(-3,3,length.out=1000), mean=1, sd=0.3), type="l", lwd=1, lty=2, col="maroon")
x<-(seq(-3,3,length.out=1000))
y<-(seq(0,1.4,length.out=1000))
plot(x,y, type = "n", main=paste("Prior and Posterior Distributions"), xlab=("θ"), ylab=("density"))
lines((seq(-3,3,length.out=1000)), dnorm(seq(-3,3,length.out=1000), mean=0, sd=10), type="l", lwd=1, lty=1, col="steelblue")
lines((seq(-3,3,length.out=1000)), dnorm(seq(-3,3,length.out=1000), mean=1, sd=0.1), type="l", lwd=1, lty=2, col="maroon")
x<-(seq(-3,3,length.out=1000))
y<-(seq(0,1.4,length.out=1000))
plot(x,y, type = "n", main=paste("Prior and Posterior Distributions"), xlab=("θ"), ylab=("density"))
lines((seq(-3,3,length.out=1000)), dnorm(seq(-3,3,length.out=1000), mean=0, sd=2), type="l", lwd=1, lty=1, col="steelblue")
lines((seq(-3,3,length.out=1000)), dnorm(seq(-3,3,length.out=1000), mean=1, sd=0.3), type="l", lwd=1, lty=2, col="maroon")
x<-(seq(-3,3,length.out=1000))
y<-(seq(0,1.4,length.out=1000))
plot(x,y, type = "n", main=paste("Prior and Posterior Distributions"), xlab=("θ"), ylab=("density"))
lines((seq(-3,3,length.out=1000)), dnorm(seq(-3,3,length.out=1000), mean=0.5, sd=0.3), type="l", lwd=1, lty=1, col="maroon")
d<-density(dnorm(seq(-3,3,length.out=1000), kernel="gaussian"))
polygon(c(0, rev(d$x[d$x<0])), c(0, rev(d$y[d$x<0])), col="maroon", border=NA)
d<-density(dnorm(seq(-3,3,length.out=1000))) #, kernel="gaussian"))
polygon(c(0, rev(d$x[d$x<0])), c(0, rev(d$y[d$x<0])), col="maroon", border=NA)
plot(x,y, type = "n", main=paste("Prior and Posterior Distributions"), xlab=("θ"), ylab=("density"))
lines((seq(-3,3,length.out=1000)), dnorm(seq(-3,3,length.out=1000), mean=0.5, sd=0.3), type="l", lwd=1, lty=1, col="maroon")
cord.x <- c(-3,seq(-3,0,0.01),0)
cord.y <- c(0,dnorm(seq(-3,0,0.01)),0)
polygon(cord.x,cord.y,col="maroon")
plot(x,y, type = "n", main=paste("Prior and Posterior Distributions"), xlab=("θ"), ylab=("density"))
lines((seq(-3,3,length.out=1000)), dnorm(seq(-3,3,length.out=1000), mean=0.5, sd=0.3), type="l", lwd=1, lty=1, col="maroon")
cord.x <- c(-3,seq(-3,0,0.01),0)
cord.y <- c(0,dnorm(seq(-3,0,0.01), mean=0.5, sd=0.3),0)
polygon(cord.x,cord.y,col="maroon")
# prior-posterior example
plot(x,y, type = "n", main=paste("Prior and Posterior Distributions"), xlab=("θ"), ylab=("density"))
lines((seq(-3,3,length.out=1000)), dnorm(seq(-3,3,length.out=1000), mean=0, sd=2), type="l", lwd=1, lty=2, col="steelblue")
lines((seq(-3,3,length.out=1000)), dnorm(seq(-3,3,length.out=1000), mean=1, sd=0.3), type="l", lwd=1, lty=1, col="maroon")
x<-(seq(-3,3,length.out=1000))
y<-(seq(0,1.4,length.out=1000))
plot(x,y, type = "n", main=paste("Prior and Posterior Distributions"), xlab=("θ"), ylab=("density"))
lines((seq(-3,3,length.out=1000)), dnorm(seq(-3,3,length.out=1000), mean=0.5, sd=0.3), type="l", lwd=1, lty=1, col="maroon")
cord.x <- c(-3,seq(-3,0,0.01),0)
cord.y <- c(0,dnorm(seq(-3,0,0.01), mean=0.5, sd=0.3),0)
polygon(cord.x,cord.y,col="maroon")
# prior-posterior example
plot(x,y, type = "n", main=paste("Prior and Posterior Distributions"), xlab=("θ"), ylab=("density"))
lines((seq(-3,3,length.out=1000)), dnorm(seq(-3,3,length.out=1000), mean=0, sd=2), type="l", lwd=1, lty=2, col="steelblue")
lines((seq(-3,3,length.out=1000)), dnorm(seq(-3,3,length.out=1000), mean=1, sd=0.3), type="l", lwd=1, lty=1, col="maroon")
#library(mvtnorm)
#library(arm)
library(BRugs)
#library(R2OpenBUGS)
#library(coda)
#library(car)
library(foreign)
# Change this to your working data directory.  Note this path makes Dropbox work at least on a windows machine
setwd("~/../Dropbox/UCR/research/bayes/funglee/persuasion/OBOE/Replication/ResultsInPaper/Tutorial")
dataset<-read.dta(file.choose()) # read in a Stata file "tutorial_data.dta"
# globally set constants and variables
n_respondents <- length(dataset$groupid)  # number of units in the analysis
n_sites <- 3  # number of "sites" exogenous variables
xpostO1<-dataset$xpostO1  # continuous outcome variable
sites<-as.matrix(cbind(dataset$site2, dataset$site3))  # collecting the exogenous varaiables in a matrix object
#  The data and inits objects are used below to create the data and initial value files to send to OpenBUGS
data<-list("n_respondents", "n_sites", "xpostO1", "sites") # to set up the data file, creates a list of the variable and constant names
inits <- function() {  # this is a function that generates random initial values
list(beta.0=rnorm(1), beta.3=rnorm(n_sites-1), tau.xpostO1=runif(1))}
bugs.data(data, dir=getwd(), digits=5, data.file="data.txt")
bugs.data(inits(), dir=getwd(), digits=3, data.file="inits1.txt")
bugs.data(inits(), dir=getwd(), digits=3, data.file="inits2.txt")
bugs.data(inits(), dir=getwd(), digits=3, data.file="inits3.txt")
library(R2OpenBUGS)
bugs.data(data, dir=getwd(), digits=5, data.file="data.txt")
bugs.data(inits(), dir=getwd(), digits=3, data.file="inits1.txt")
bugs.data(inits(), dir=getwd(), digits=3, data.file="inits2.txt")
bugs.data(inits(), dir=getwd(), digits=3, data.file="inits3.txt")
?rnorm
qnorm(0.5)
qnorm(1)
qnorm(.9)
?curve
curve(qnorm(x), from=.1, to=.9)
qnorm(.9999)
qnorm(-.9999)
qnorm(.000001)
# plot normal curves on one figure
x<-(seq(-3,3,length.out=1000))
y<-(seq(0,1.4,length.out=1000))
plot(x,y, type = "n", main=paste("Posterior Distribution for a Parameter"), xlab=("Beta"), ylab=("density"))
lines((seq(-3,3,length.out=1000)), dnorm(seq(-3,3,length.out=1000), mean=0.5, sd=0.3), type="l", lwd=1, lty=1, col="maroon")
cord.x <- c(-3,seq(-3,0,0.01),0)
cord.y <- c(0,dnorm(seq(-3,0,0.01), mean=0.5, sd=0.3),0)
polygon(cord.x,cord.y,col="maroon")
# prior-posterior example
plot(x,y, type = "n", main=paste("Prior and Posterior Distributions"), xlab=("Beta"), ylab=("density"))
lines((seq(-3,3,length.out=1000)), dnorm(seq(-3,3,length.out=1000), mean=0, sd=2), type="l", lwd=2, lty=2, col="steelblue")
lines((seq(-3,3,length.out=1000)), dnorm(seq(-3,3,length.out=1000), mean=1, sd=0.3), type="l", lwd=2, lty=1, col="maroon")
library(mvtnorm)
library(arm)
library(BRugs)
library(R2OpenBUGS)
library(coda)
library(car)
library(foreign)
# Change this to your working directory.  Note this path makes Dropbox work at least on a windows machine
setwd("~/../Dropbox/UCR/research/bayes/funglee/persuasion/OBOE/Replication/ResultsInPaper/Tutorial")
dataset<-read.dta(file.choose()) # read in a Stata file "tutorial_data.dta"
# create the matrix of group assignments
TableAdj=matrix(data=0, nrow=length(dataset$groupid), ncol=length(dataset$groupid))
for (i in 1:length(dataset$groupid)) {
for (j in 1:length(dataset$groupid)) {
if (i!=j) {
if (dataset$groupid[i]==dataset$groupid[j]) {
TableAdj[i,j]<-1
}}}}
# Create Vector of group mappings to read into Congdon programs:
TableMap<-list(map=NULL)
for (i in 1:length(dataset$groupid)) {
for (j in 1:length(dataset$groupid)) {
if (TableAdj[i,j]==1) TableMap<-list(map=c(TableMap$map, j))
}}
# count how many people (other than i) are at i's table and add that to i-1's count
TableC<-list(C=0)
nadj<-0
for (i in 1:length(dataset$groupid)) {
nadj<-sum(TableAdj[i,])+nadj
TableC<-list(C=c(TableC$C, nadj))
}
map <- TableMap$map
C <- TableC$C
one<-rep(1, length(dataset$groupid))
numTableNeigh<-sum(as.numeric(t(TableAdj%*%one)))
C
numTableNeigh
length(TableC)
length(TableC$C)
